package com.hunttown.mes.manage.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.Map;

public class ShiroFilterUtils {
    final static Class<? extends ShiroFilterUtils> CLAZZ = ShiroFilterUtils.class;

    // 登录页面
    public static final String LOGIN_URL = "/logon";

    // 没有权限提醒
    public final static String UNAUTHORIZED = "/unauthorized";

    /**
     * 是否是Ajax请求
     *
     * @param request
     * @return
     */
    public static boolean isAjax(ServletRequest request) {
        return "XMLHttpRequest".equalsIgnoreCase(((HttpServletRequest) request).getHeader("X-Requested-With"));
    }

    /**
     * Response 输出JSON
     *
     * @param response  response
     * @param resultMap 结果
     */
    public static void out(ServletResponse response, Map<String, String> resultMap) {

        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            out.println(resultMap.get("message"));

        } catch (Exception e) {
            System.out.println("输出JSON报错。");

        } finally {
            if (null != out) {
                out.flush();
                out.close();
            }
        }
    }
}
